use rand::Rng;
use std::{cmp::Ordering, io};

fn main() {
    println!("猜数游戏(1-100)!!!");
    // 生成随机数
    let secret_number = rand::thread_rng().gen_range(1, 101);
    // println!("神秘数字是: {}", secret_number);
    loop {
        println!("猜测1个数");
        let mut guess = String::new();
        // 读取io输入
        io::stdin().read_line(&mut guess).expect("无法读取行");
        // 把输入的字符串转成数字
        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };
        println!("你猜测的数是: {}", guess);
        // 比较大小
        match guess.cmp(&secret_number) {
            Ordering::Less => println!("too small"),
            Ordering::Greater => println!("too big"),
            Ordering::Equal => {
                println!("you win");
                break;
            }
        }
    }
}
